/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.file;
import  java.util.ArrayList;
import  java.util.StringTokenizer;
import  java.util.Properties;
import  java.io.IOException;
import  java.io.BufferedReader;
import  java.io.FileReader;
import  java.io.FileWriter;
import  java.io.PrintWriter;
import  java.io.IOException;
import  java.io.File;
import  java.io.FileInputStream;
import  org.apache.log4j.Logger;
import  com.evolve.webbot.bean.LiverpoolDayBean;
import java.util.Iterator;
/**
 *
 * @author DarthAE
 */
public class TxtLiverpoolDia 
{
    private String filePath;
       static Logger log = Logger.getLogger( TxtLiverpoolDia.class ); 

    public TxtLiverpoolDia(){}
    
    public TxtLiverpoolDia(String filePath)
    {
        this.filePath=filePath;
    }
    
    public ArrayList<LiverpoolDayBean>  readLiverpoolTxtFile(String path,
                                                             ArrayList<String> listaEstados,
                                                             ArrayList<String> listaTemporada)
    {
      ArrayList<LiverpoolDayBean> liverpoolRecordList= new ArrayList<LiverpoolDayBean>();  
      ArrayList<String>valueList =  new ArrayList<String>();
      
      boolean inicioCant = false;
      LiverpoolDayBean liverpool = null;
      String  desc       = "";
      String  centro     = "";
      String  value      = null;
      boolean esEstado   = false;
      StringTokenizer tokens = null;
      int i=0,j=0,l=0,m=0;
            
      try {
             BufferedReader br = new BufferedReader(new FileReader(path));
             String linea = br.readLine();

             while (linea != null) 
             {

               if(linea.contains("Resultado"))
               {
                   inicioCant = false;
               }    
               
               if(inicioCant)
               { 
                 
                 if(!linea.contains("Centro") &&
                    !linea.contains("No existen datos adecuados"))
                 {
                     liverpool = new LiverpoolDayBean();
                     
                     if(i == 0)
                     {
                         boolean fallo = true;
                         
                         tokens = new StringTokenizer(linea);
                         
                         System.out.println(linea);
                         
                         while(tokens.hasMoreTokens() && fallo)
                         {
                           value=tokens.nextToken().trim();
                          
                           try{
                                 double doubleValue = Double.parseDouble(value);
                                 
                                 if(m>0)
                                 {
                                   fallo = false;
                                 }
                                 else 
                                 if(m==0)
                                 {
                                     linea=linea.replace(value.trim(),"");
                                     centro=centro+value+" ";
                                 }
                                 
                              }catch(Exception e){ centro=centro+value+" ";
                                                   linea=linea.replace(value.trim(),"");
                                                   System.out.println("error");}
                           m++;
                         }
                         //System.out.println(centro);
                         linea = linea.replace(centro.trim(),"").trim();
                         System.out.println("-"+linea);
                         liverpool.setCentro(centro.trim());
                         i=1;
                     }

                     if(i>0)
                     {
                        liverpool.setCentro(centro);
                        
                        tokens = new StringTokenizer(linea);
                      
                        while(tokens.hasMoreTokens() && l<1)
                        {
                         value=tokens.nextToken().trim();
                         
                         linea = (linea.replace(value,"")).trim();
                         
                         liverpool.setArticulo(value.trim());
                         
                         l++;
                        }
                        l=0;
                        
                        while(l < listaEstados.size() && !esEstado)
                        {
                            if(linea.contains((listaEstados.get(l)).trim()))
                            {
                                /*System.out.println("encontrado");*/
                                esEstado = true;
                                linea = linea.replace(listaEstados.get(l),"");
                                liverpool.setEstado(listaEstados.get(l).trim());
                            }
                            l++;
                        }
                        l=0;
                        esEstado = false;
                        
                        while(l < listaTemporada.size() && !esEstado)
                        {
                            if(linea.contains((listaTemporada.get(l)).trim()))
                            {
                                /*System.out.println("encontrado");*/
                                esEstado = true;
                                linea = linea.replace(listaTemporada.get(l),"");
                                liverpool.setTemporada(listaTemporada.get(l).trim());
                            }
                            l++;
                        }
                        l=0;
                        esEstado = false;  
                        
                        tokens = new StringTokenizer(linea);
                        value  = "";
                        
                        while(tokens.hasMoreTokens())
                        {
                         value=tokens.nextToken().trim();
                         valueList.add(value);
                        }                        
                        
                        for(int k=valueList.size()-1;k>(valueList.size()-5);k--)
                        {
                            /*System.out.println(valueList.get(k));*/
                            
                            switch(j)
                            {
                                case 0: liverpool.setVentas(valueList.get(k).trim());
                                        break;
                                case 1: liverpool.setVentasUnidades(valueList.get(k).trim());
                                        break;
                                case 2: liverpool.setEanUpc(valueList.get(k).trim());
                                        break;
                                case 3: liverpool.setModelo(valueList.get(k).trim());
                                        break;
                                default: System.out.println("valor nulo");
                                         break;
                                         
                            }
                            j++;
                        }
                        
                        for(int k=0;k<(valueList.size()-4);k++)
                        {
                            desc=desc+valueList.get(k)+" ";
                        }
                        liverpool.setDescripcion(desc);
                        desc="";
                        j=0;
                        valueList.removeAll(valueList);
                        
                     }
                     
                     i++;
                 } 
                 System.out.println(liverpool);
                 liverpoolRecordList.add(liverpool);
               }               
               
               if(linea.contains("Ventas Diarias"))
               {
                    inicioCant = true; 
               }   
               
               linea = br.readLine();
             }
             
           } catch(IOException e){}
      
      return liverpoolRecordList;
    }
    
    public void writeSanbornsDataFile(ArrayList<LiverpoolDayBean> liverpoolRecordList)
    {
        try {
               FileWriter  outFile = new FileWriter(this.filePath.replaceAll("vyc","data"));
               PrintWriter out     = new PrintWriter(outFile);
              
               for(int i=0;i<liverpoolRecordList.size();i++)
               {
                 out.println(liverpoolRecordList.get(i).toString());
               }
              
               out.close();
               
            } catch (IOException e){e.printStackTrace();}
    }
    
    public String getTokens(String linea)
    {
        String value = null;
        
        StringTokenizer tokens = new StringTokenizer(linea);

        while(tokens.hasMoreTokens())
        {
          value=tokens.nextToken().trim();
          System.out.println(value);
        }
        
        return value;
    }
    
    public LiverpoolDayBean getValues(String linea,
                                      LiverpoolDayBean liverpool)
    {
        /*LiverpoolDayBean liverpool = new LiverpoolDayBean();*/
        StringTokenizer tokens = null;
        boolean esDesc    = false;
        String  value     = "";
        String  nuevaLinea= "";
        int     i         =  0;
        int     k         =  0;
        int     l         =  0;
        
        linea  = invierteCadena(linea);
        tokens = new StringTokenizer(linea);       

        while(tokens.hasMoreTokens())
        {
          value = tokens.nextToken().trim();
          
          if(i==0)
          {
              liverpool.setVentas(invierteCadena(value));
          }
          else
          if(i>0 && i<2)
          {
              liverpool.setVentasUnidades(invierteCadena(value));
          } 
          else
          {
              nuevaLinea = nuevaLinea + value+" ";
          }
          i++;
        } 
        
        System.out.println(liverpool);
        return liverpool;
    } 
    
    public String invierteCadena(String linea)
    {
        String lineaInversa = "";
        
        for (int x=linea.length()-1;x>=0;x--)
        {
            lineaInversa = lineaInversa + linea.charAt(x);
        }		
        
        return lineaInversa;
    }
    
    public ArrayList<String> obtenListaEstadoTemporada(String path,String opcion)
    {
        ArrayList<String> listaEstadoTemporada = new ArrayList<String>();
        String estados = null;
        Properties properties = new Properties();
        
        try{
             FileInputStream fis = new FileInputStream(path/*+File.separator+"LiverpoolBean.properties"*/);
             properties.load(fis);
             estados = properties.getProperty(opcion);
            
             fis.close();
            
             StringTokenizer stk = new StringTokenizer(estados,",");
            
             while(stk.hasMoreElements())
             {
                String value = stk.nextToken();
                listaEstadoTemporada.add(value);
             }   
            
           }catch(IOException ioex){}
        
        return listaEstadoTemporada;
    }
    
    public ArrayList<String> obtenCentrosLiverpool(String path)
    {
        ArrayList<String> listaCentros = new ArrayList<String>();
        String estados = null;
        Properties properties = new Properties();
        
        try{
             FileInputStream fis = new FileInputStream(path/*+File.separator+"LiverpoolBean.properties"*/);
             properties.load(fis);
             estados = properties.getProperty("liverpoolBean.centros");
            
             fis.close();
            
             StringTokenizer stk = new StringTokenizer(estados,",");
            
             while(stk.hasMoreElements())
             {
                String value = stk.nextToken();
                listaCentros.add(value.trim());
             }   
            
           }catch(IOException ioex){}
        
        return listaCentros;
    }    
    
    public void writeLiverMesDataFile(String path,
                                      ArrayList<LiverpoolDayBean> liverMesRecordList)
    {
        try {
                if(!liverMesRecordList.isEmpty())
                {
                    
                   FileWriter  outFile = new FileWriter(path.replaceAll("vyc","data"));
                   PrintWriter out     = new PrintWriter(outFile);
                   
                   Iterator iter = liverMesRecordList.iterator();
                   
                   while(iter.hasNext())
                   {
                       LiverpoolDayBean bean = (LiverpoolDayBean)iter.next();
                       
                       if(bean!=null)
                       {
                           out.println(bean.toString());
                       }                       
                   }
                   
                   out.close();                                      
                }               
            } catch (IOException e){e.printStackTrace();}
    } 
}
